﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Comm.PublicClass;
using WsProtocol.PublicClass;

namespace Wasion.HHU.Services
{
    public delegate void DoChangeMeter(string meterid);  //表计信息改变后需要执行的委托

    public class PtlBaseMeter : IDisposable
    {
        private string meterid = string.Empty;  //表id
        private int sno;   //表位
        private string metertype = string.Empty;  //表型
        private string meterno = string.Empty;  //表号
        private string meteraddr = string.Empty;  //表地址
        private string password = string.Empty;   //密码


        public string MeterID //唯一ID 
        {
            get { return meterid; }
            set
            {
                meterid = value;
                SetMeterID();
            }
        }

        public int SNO //表位
        {
            get { return sno; }
            set
            {
                sno = value;
                SetMeterSNO();
            }
        }

        public string MeterType //表型
        {
            get { return metertype; }
            set
            {
                metertype = value;
                SetMeterType();
            }
        }
        
        public string MeterNO 
        {
            get { return meterno; }
            set 
            {
                meterno = value;
                SetMeterNO();
            }
        } //表号

        //表地址
        public string MeterCommAddr
        {
            get { return meteraddr; }
            set 
            {
                meteraddr = value;
                SetMeterCommAddr();
            }
        }

        //表密码
        public string PassWord
        {
            get { return password; }
            set
            {
                password = value;
                SetPassWord();
            }
        }
        public string MeterName { get; set; }  //表名
        public string InstAddr { get; set; }
        public string ProjectCode { get; set; }  //所属项目编号
        public string ProjGroup { get; set; }  //所属项目分组  用于同一项目下不同规格的表分组
        public string OrderNo { get; set; }  //内部订单号
        public string MeterAssetNO { get; set; }  //资产编号        
        public string MClass { get; set; }  //精度等级
        public string Voltage { get; set; }  //电压规格
        public string FCurrent { get; set; }  //电流规格
        public string Frequency { get; set; }  //频率
        public string PConstant { get; set; }  //有功脉冲常数
        public string QConstant { get; set; }  //无功脉冲常数
        public string FPrecision { get; set; }    //有功精度等级  
        public string QPrecision { get; set; }    //无功精度等级
        public string ConnectMode { get; set; }  //接线方式 
        public string IPAndPort { get; set; }  //IP和端口  用于socket通信
        public string MeterDesc { get; set; }

        public string ErrInfo { get; set; }  //错误信息
        public string Status { get; set; }  //当前工作状态

        public object ResultShow { get; set; }  //返回结果展示
        public object RanResultShow { get; set; }  //任意命令返回结果展示 
        public object UpdateMeterShow { get; set; }  //表程序升级结果展示
        public object UploadShow { get; set; }  //主动上传结果展示   2019-8-19  shaunwang
        //public SourceMeterMode commMode { get; set; }  //默认为串口
        public CommBaseInfo CommInfo { get; set; }  //通信相关属性        
        public CheckState checkState { get; set; }  //选中状态 
        public static DoChangeMeter EventChangeMeter;

        public object ListenAnaPtl = null;  //记录每次监听解析后新生成的解析对象

        //加密相关属性  暂空缺
        public PtlBaseMeter()
        {
            sno = 1;
            ProjectCode = string.Empty;
            MeterDesc = string.Empty;
            ProjGroup = string.Empty;
            //commMode = SourceMeterMode.串口485;
            CommInfo = new CommBaseInfo();

            CreateMeter();
        }

        public void Dispose()
        { 
        
        }

        //复制自身
        public virtual PtlBaseMeter Copy()
        {
            //新建一个
            PtlBaseMeter newmeter = new PtlBaseMeter();

            //复制属性
            PropertyInfo[] fromFields = typeof(PtlBaseMeter).GetProperties();
            PubFunction.SetProperties(fromFields, this, newmeter);
            return newmeter;
        }


        public void DoAfterChange()
        {
            if (EventChangeMeter != null)
                EventChangeMeter(this.meterid);
        }

        //构造时执行
        protected virtual void CreateMeter()
        { 
        
        }

        //设表ID虚方法
        protected virtual void SetMeterID()
        {

        }

        //设表位虚方法
        protected virtual void SetMeterSNO()
        {

        }

        //设表型虚方法
        protected virtual void SetMeterType()
        {

        }


        //设表号虚方法
        protected virtual void SetMeterNO() 
        {
            CommInfo.SNO = this.SNO;
        }

        //设地址虚方法
        protected virtual void SetMeterCommAddr()  
        {
        
        }

        //设密码虚方法
        protected virtual void SetPassWord()  
        {
        
        }
    }

    public class CommBaseInfo
    {
        public string CommName;

        public CommBaseInfo()
        { }

        public int SNO { get; set; }
        public string IP { get; set; }
        public int Port { get; set; }
        public string MACAddr { get; set; }
        public SourceMeterMode ConnMode { get; set; }
    }

    public enum SourceMeterMode
    {
        串口485 = 1,
        光口通信 = 2,
        台体红外 = 3,
        台体485 = 4,
        TCP直连 = 6,
        UDP直连 = 7,
        威胜蓝牙 = 8
    }
}
